package com.example.appstudy2405;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.appstudy2405.utils.ReadFileLocal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivityTypingSimple extends AppCompatActivity {
    private String[] sentences = {"桌子", "SUV", "蛋糕", "苹果", "毛线衣", "猕猴桃", "茶杯"};
    private String[] inputs = {"200 元", "150000 元","150 元","9.9 元","55 元","15.5 元", "35 元"};

    private static final int REQUEST_CODE_STORAGE_PERMISSION = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_typing_simple);

        // 检查本地文件读取权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_STORAGE_PERMISSION);
        }

//        List<Map<String, Object>> list = new ArrayList<>();
//        for (int i = 0; i<sentences.length; ++i) {
//            Map<String, Object> item = new HashMap<>();
//            item.put("sentence", sentences[i]);
//            item.put("input", inputs[i]);
//            list.add(item);
//        }
        List<Map<String, Object>> typingListMap = createTypingListMap("/Documents/001&002－Excuse Me.lrc");

        SimpleAdapter typingAdapter = new SimpleAdapter(this, typingListMap,
                R.layout.article_sentence_type,
                new String[]{"sentence", "input"},
                new int[]{R.id.tv_article_sentence, R.id.et_article_sentence_type});

        ListView lv_typing = findViewById(R.id.lv_type);
        lv_typing.setAdapter(typingAdapter);
    }

    private List<Map<String, Object>> createTypingListMap(String filePath) {
        ReadFileLocal readFileLocal = new ReadFileLocal();
        String articleContent = readFileLocal.readFileFromLocal(filePath);
        String[] lines = articleContent.split("\n");
        // 构建待打印文本
        String[] sentences;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            sentences = Arrays.stream(lines)
                    .filter(line -> line.startsWith("[00:"))
                    .map(line -> line.replaceFirst("\\[00:\\d+\\.\\d+\\]", ""))
                    .toArray(String[]::new);
        } else {
            ArrayList<String> sentencesList = new ArrayList<>();
            for (String line: lines) {
                if (line.startsWith("[00:")) {
                    int startIndex = line.indexOf(']') + 1;
                    sentencesList.add(line.substring(startIndex).trim());
                }
            }
            sentences = sentencesList.toArray(new String[0]);
        }
        for (String sentence : sentences) {
            Log.i("typing", "createTypingListMap: sentence: " + sentence);
        }

        List<Map<String, Object>> listMap = new ArrayList<>();

        for (int i = 0; i < sentences.length; ++i) {
            Map<String, Object> item = new HashMap<>();
            item.put("sentence", sentences[i]);
            item.put("input", "");
            listMap.add(item);
        }
        return listMap;
    }


}